import CommonHook from "@/hook/commonHook";
import FormHook from "@/hook/formHook";
import { cloneDeep } from "lodash";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formDataRef, formState, emit, materialOpts } = state;
  const { resetFields } = FormHook({ formState });
  const { getLabelByDict } = CommonHook();
  const onFinish = () => {
    switch (formState.type) {
      case 1:
        formState.conditionDesc = `报关方式为: ${getLabelByDict("CUSTOMS_DECLARATION_METHOD", formState.typeValue)}`;
        break;
      case 2:
        formState.conditionDesc = `品名数超: ${formState.baseProductSize}个，每超出${formState.exceedSize}个`;
        break;
      case 3:
        formState.conditionDesc = `${getLabelByDict("TAX_PAYMENT_METHOD", formState.typeValue)}`;
        break;
      case 4:
        formState.conditionDesc = `${getMaterialLabel(formState.typeValue)}`;
        break;
      case 5:
        formState.conditionDesc = `起运地为: ${formState.conditionDesc}`;
        formState.typeValue = formState.typeValue.join(",");
        break;
      case 6:
        formState.conditionDesc = "";
        break;
    }
    emit("submit", cloneDeep(formState));
  };
  const cancelHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  const getMaterialLabel = (id: string) => {
    const { material } = materialOpts.value.find((t: any) => t.id === id) || {};
    return material;
  };
  const changeUnitHandle = () => {
    if (Number(formState.unit) === 1) formState.value = 1;
    else formState.value = undefined as unknown as number;
  };
  const changeAddressHandle = (value: any, selectedOptions: any) => {
    const address = selectedOptions.map((t: any) => t.name);
    formState.conditionDesc = address.join("/");
  };
  return {
    onFinish,
    cancelHandle,
    resetFields,
    changeUnitHandle,
    changeAddressHandle
  };
}
